# -*- coding: utf-8 -*-

__author__ = "Sérgio Severino, 9733"
__version__ = "v1.0 from 2/12/2011"


from database import Database, DB
from operator import itemgetter


class HtmlFile:
    '''
    This class creates html files
    based on the received Database object
    '''
    
    @staticmethod
    def createHtmlOfTeachersPerEstablishmentPerYear( data ):
        '''
        This method receives a Database object
        then writes a html file with the list sorted by name
        per establishment per year
        '''
    
        #sorts the list by year, establishment, then by name
        listTeachers = sorted(data.getDBList(), key=itemgetter(DB.YEAR,
                                                               DB.ESTABLISHMENT_NAME,
                                                               DB.NAME))
        
        htmlFile = open('index.html', 'w')
        htmlFile.write('<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body>')
        
        htmlFile.write("<h1>Teachers per establishment per year</h1>")
        
        year = ""
        establishment = ""
        for line in listTeachers:

            #writes the year only the first time
            if year != line[DB.YEAR]:
                htmlFile.write("<h2>Year %s</h2>" % line[DB.YEAR])
                pass
            year = line[DB.YEAR]
        
            #writes the establishment only the first time
            if establishment != line[DB.ESTABLISHMENT_NAME]:
                htmlFile.write("<h3>Establishment: %s</h3>" % line[DB.ESTABLISHMENT_NAME])
                pass
            establishment = line[DB.ESTABLISHMENT_NAME]
            
            htmlFile.write("<li>%s</li>" % line[DB.NAME])
            pass
        htmlFile.write("</body> </html>")
        htmlFile.close()
        pass


    @staticmethod
    def createHtmlOfTeachersPerCategoriePerEstablishmentPerYear( data ):
        '''
        This method receives a Database object
        then writes a html file with the list sorted by name
        per categorie per establishment per year
        '''

        #sorts the list by year, establishment, categorie, then by name
        listTeachers = sorted(data.getDBList(), key=itemgetter(DB.YEAR,
                                                                  DB.ESTABLISHMENT_NAME,
                                                                  DB.CATEGORIE,
                                                                  DB.NAME))
                
        htmlFile = open('index.html', 'w')
        htmlFile.write('<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body>')
        
        htmlFile.write("<h1>Teachers per categorie per establishment per year</h1>")
        
        year = ""
        establishment = ""
        categorie = ""
        for line in listTeachers:

            #writes the year only the first time
            if year != line[DB.YEAR]:
                htmlFile.write("<h2>Year %s</h2>" % line[DB.YEAR])
                pass
            year = line[DB.YEAR]
        
            #writes the establishment only the first time
            if establishment != line[DB.ESTABLISHMENT_NAME]:
                htmlFile.write("<h3>Establishment: %s</h3>" % line[DB.ESTABLISHMENT_NAME])
                pass
            establishment = line[DB.ESTABLISHMENT_NAME]

            #writes the categorie only the first time
            if categorie != line[DB.CATEGORIE]:
                htmlFile.write("<h4>Categorie: %s</h4>" % line[DB.CATEGORIE])
                pass
            categorie = line[DB.CATEGORIE]
            
            htmlFile.write("<li>%s</li>" % line[DB.NAME])
            pass
        htmlFile.write("</body> </html>")
        htmlFile.close()
        pass


    @staticmethod
    def createHtmlOfTeachersPerDegreePerEstablishmentPerYear( data ):
        '''
        This method receives a Database object
        then writes a html file with the list sorted by name
        per degree per establishment per year
        '''

        #sorts the list by year, establishment, degree, then by name
        listTeachers = sorted(data.getDBList(), key=itemgetter(DB.YEAR,
                                                                  DB.ESTABLISHMENT_NAME,
                                                                  DB.DEGREE,
                                                                  DB.NAME))
                
        htmlFile = open('index.html', 'w')
        htmlFile.write('<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body>')
        
        htmlFile.write("<h1>Teachers per categorie per establishment per year</h1>")
        
        year = ""
        establishment = ""
        categorie = ""
        for line in listTeachers:

            #writes the year only the first time
            if year != line[DB.YEAR]:
                htmlFile.write("<h2>Year %s</h2>" % line[DB.YEAR])
                pass
            year = line[DB.YEAR]
        
            #writes the establishment only the first time
            if establishment != line[DB.ESTABLISHMENT_NAME]:
                htmlFile.write("<h3>Establishment: %s</h3>" % line[DB.ESTABLISHMENT_NAME])
                pass
            establishment = line[DB.ESTABLISHMENT_NAME]

            #writes the degree only the first time
            if categorie != line[DB.DEGREE]:
                htmlFile.write("<h4>Degree: %s</h4>" % line[DB.DEGREE])
                pass
            categorie = line[DB.DEGREE]
            
            htmlFile.write("<li>%s</li>" % line[DB.NAME])
            pass
        htmlFile.write("</body> </html>")
        htmlFile.close()
        pass
